package com.iqiyi.pps.epg.core.dao.record;

import com.iqiyi.pps.epg.core.dao.base.BaseDao;
import com.iqiyi.pps.epg.core.model.record.RecordVideo;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by hejia on 2018/4/11.
 */
@Component
public class RecordVideoDao extends BaseDao<RecordVideo> {

    private static final String QUERY_RECORDVIDEO_BY_SIGNALID = "select s from RecordVideo s where s.signalId =? and taskId = ? and s.status = 1";
    private static final String UPDATE_RECORDVIDEO_BY_TASKID = "update RecordVideo set videoStatus = ? where status = 1 and taskId = ? ";

    private static final String UPDATE_DELETE_STATUS_BY_CHANNEL = "update RecordVideo set status = 0 where status = 1 and channelId = ? ";

    private static final String UPDATE_DELETE_STATUS_BY_TASK = "update RecordVideo set status = 0 where status = 1 and taskId = ? ";

    private static final String UPDATE_DELETE_STATUS_BY_TASKS = "update RecordVideo set status = 0 where status = 1 and taskId in  ";

    public RecordVideo getBySignalIdAndTaskId(long signalId, long taskId) {
        List result = createQuery(QUERY_RECORDVIDEO_BY_SIGNALID, signalId, taskId).list();
        if (result != null && result.size() > 0) {
            return (RecordVideo) result.get(0);
        } else {
            return null;
        }
    }

    public void updateByTaskId(long taskId,int videoStatus) {
        int execute = createQuery(UPDATE_RECORDVIDEO_BY_TASKID).setInteger(0, videoStatus).setLong(1,taskId).executeUpdate();
        logger.info("[updateByTaskId][taskId={}][execute={}]", taskId, execute);

    }

    public void deleteVideoByChannel(long channelId) {
        int execute = createQuery(UPDATE_DELETE_STATUS_BY_CHANNEL).setLong(0, channelId).executeUpdate();
        logger.info("[channelId={}][execute={}]", channelId, execute);
    }

    public void deleteVideoByTask(long taskId) {
        int execute = createQuery(UPDATE_DELETE_STATUS_BY_TASK).setLong(0, taskId).executeUpdate();
        logger.info("[taskId={}][execute={}]", taskId, execute);
    }

    public void deleteVideoByTasks(String taskIds) {
        int execute = createQuery(UPDATE_DELETE_STATUS_BY_TASKS + "(" + taskIds + ")").executeUpdate();
        logger.info("[taskIds={}][execute={}]", taskIds, execute);
    }

}
